package lt.hsu.product.vo.valid;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.List;
import java.util.regex.Pattern;

public class ListValueRegexValidator implements ConstraintValidator<ListValueRegex, List<String>> {

    private Pattern pattern;

    @Override
    public void initialize(ListValueRegex constraintAnnotation) {
        pattern = Pattern.compile(constraintAnnotation.pattern());
    }

    @Override
    public boolean isValid(List<String> values, ConstraintValidatorContext context) {
        if (values == null) {
            return true; // null values are valid
        }
        for (String value : values) {
            if (value == null || !pattern.matcher(value).matches()) {
                return false;
            }
        }
        return true;
    }
}
